package org.geektimes.projects.user.web.listener;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.management.IndexShow;
import org.geektimes.projects.user.management.UserManager;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.lang.management.ManagementFactory;

public class MBeanInitializerListener implements ServletContextListener {
    /**
     * Receives notification that the web application initialization
     * process is starting.
     *
     * <p>All ServletContextListeners are notified of context
     * initialization before any filters or servlets in the web
     * application are initialized.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being initialized
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        final ServletContext servletContext = sce.getServletContext();
        // 获取平台 MBean Server
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        // 为 UserMXBean 定义 ObjectName
        ObjectName objectName = null;
        ObjectName indexShowName = null;
        try {
            objectName = new ObjectName("org.geektimes.projects.user.management:type=User");
            indexShowName = new ObjectName("org.geektimes.projects.user.management:type=Index");
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }
        // 创建 UserMBean 实例
        User user = new User();
        final IndexShow indexShowMBean = createIndexShowMBean();
        try {
            mBeanServer.registerMBean(createUserMBean(user), objectName);
            mBeanServer.registerMBean(indexShowMBean,indexShowName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        servletContext.setAttribute("indexShow",indexShowMBean);
        //读 http://localhost:8080/jolokia/read/org.geektimes.projects.user.management:type=User
        //写 http://localhost:8080/jolokia/write/org.geektimes.projects.user.management:type=User/Id/100
        //操作 http://localhost:8080/jolokia/exec/org.geektimes.projects.user.management:type=User/toString()
        //开启debug http://localhost:8080/jolokia/write/jolokia:type=Config/Debug/true
        //search http://localhost:8080/jolokia/search/jolokia:type=*
        //search http://localhost:8080/jolokia/search/org.geektimes.projects.user.management:type=*
        //list http://localhost:8080/jolokia/list/java.lang/type=Memory
        //list http://localhost:8080/jolokia/list/org.geektimes.projects.user.management/type=User
        //写 http://localhost:8080/jolokia/write/org.geektimes.projects.user.management:type=Index/Text/中文

//读 http://localhost:8080/jolokia/read/
//写 http://localhost:8080/jolokia/write/
//操作 http://localhost:8080/jolokia/exec/
//search http://localhost:8080/jolokia/search/
//list http://localhost:8080/jolokia/list/


        new Thread(()->{
            while (true) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(user);
                servletContext.setAttribute("indexShow",indexShowMBean);
            }
        }).start();

    }

    private static Object createUserMBean(User user) throws Exception {
        return new UserManager(user);
    }

    private static IndexShow createIndexShowMBean(){
        return new IndexShow();
    }

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     *
     * <p>All servlets and filters will have been destroyed before any
     * ServletContextListeners are notified of context
     * destruction.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being destroyed
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
